import type { SelectedFile } from '#shared/files/files.model';

export type CodeTaskStatus =
	| 'initializing' // Session created, preparing workspace
	| 'file_selection_review' // AI-generated file selection is ready for review
	| 'updating_file_selection' // AI is updating file selection based on user feedback
	| 'generating_design' // AI is generating/updating the design
	| 'design_review' // AI-generated design is ready for review
	| 'coding' // AI is actively writing or modifying code (from design or review comments)
	| 'code_review' // Waiting for user feedback on the generated code changes
	| 'committing' // AI is make commits, pushing changes, creating a merge/pull request if required
	| 'ci_monitoring' // Monitoring CI/CD pipeline
	| 'feedback' // Waiting on human feedback due to difficulties
	| 'completed' // Process finished successfully (including potential CI success)
	| 'error'; // An unrecoverable error occurred during the process

/**
 * A CodeTask represents an AI-assisted development workflow
 */
export interface CodeTask {
	id: string; // Primary key, ideally a UUID
	userId: string; // To associate with a user
	title: string;
	instructions: string;
	repositorySource: 'local' | 'github' | 'gitlab'; // Source of the repository
	repositoryId: string; // Identifier for the repository (e.g., local path, 'owner/repo', 'group/project')
	repositoryName?: string; // Optional: User-friendly name (e.g., 'my-cool-project')
	targetBranch: string; // The existing branch to base the work on and potentially merge into
	workingBranch: string; // The name of the branch to perform work on (can be new or existing)
	createWorkingBranch: boolean; // Whether the workingBranch needs to be created
	useSharedRepos: boolean; // Flag indicating if shared repository storage should be used
	status: CodeTaskStatus; // Use the exported type
	lastAgentActivity: number; // Time of the last significant action by an agent
	fileSelection?: SelectedFile[]; // Array of files selected by the file selection agent for the task - Uses MODIFIED SelectedFile
	designAnswer?: string;
	codeDiff?: string; // The generated code changes in diff format
	commitSha?: string; // The SHA hash of the commit generated by the Code task
	pullRequestUrl?: string; // The URL of the pull/merge request created (if applicable)
	ciCdStatus?: 'pending' | 'running' | 'success' | 'failed' | 'cancelled'; // Status of the associated CI/CD pipeline run
	ciCdJobUrl?: string; // Direct link to the specific CI/CD job or pipeline run
	ciCdAnalysis?: string; // AI-generated analysis of CI/CD failure logs (if applicable)
	ciCdProposedFix?: string; // AI-proposed code changes to fix a CI/CD failure (if applicable)
	createdAt: number;
	updatedAt: number;
	/** History of agents ids run for this code task */
	agentHistory?: string[];
	error?: string | null;
}

// --- Data Transfer Objects (DTOs) ---

// Data needed to create a new code task (typically from user input)
export type CreateCodeTaskData = Pick<
	CodeTask,
	'title' | 'instructions' | 'repositorySource' | 'repositoryName' | 'targetBranch' | 'workingBranch' | 'createWorkingBranch' | 'useSharedRepos'
> & {
	// For creation, repositoryFullPath is made optional.
	// If not provided directly, it might be derived from repositoryName for certain sources.
	repositoryFullPath?: string;
};

// Data allowed for generic updates via the updateCodeTask method
// Excludes fields that should not be directly updated or are immutable
export type UpdateCodeTaskData = Partial<
	Pick<
		CodeTask,
		| 'title'
		| 'instructions'
		| 'repositoryName'
		| 'useSharedRepos'
		| 'status'
		| 'lastAgentActivity'
		| 'fileSelection'
		| 'designAnswer'
		| 'codeDiff'
		| 'commitSha'
		| 'pullRequestUrl'
		| 'ciCdStatus'
		| 'ciCdJobUrl'
		| 'ciCdAnalysis'
		| 'ciCdProposedFix'
		| 'updatedAt'
		| 'agentHistory'
		| 'error'
	>
> & {
	filesToAdd?: string[]; // Array of file paths to add
	filesToRemove?: string[]; // Array of file paths to remove
};

// Specific data structure for requesting code revisions based on user review comments
export interface UpdateCodeReviewData {
	reviewComments: string; // User's comments or instructions for revising the code
}

// Specific data structure for the final commit action, providing commit details
export interface CommitChangesData {
	commitTitle: string; // The title for the git commit
	commitMessage: string; // The detailed message for the git commit
}

// CodeTask Preset Definitions
export interface CodeTaskPreset {
	id: string;
	userId: string;
	name: string;
	config: Omit<CreateCodeTaskData, 'title' | 'instructions'>;
	createdAt: number;
	updatedAt: number;
}

/** Data for updating file selection based on a user prompt */
export interface UpdateSelectionPromptData {
	prompt: string;
}

/** Data for generating a detailed design, potentially with variations */
export type GenerateDesignData = {
	instructions: string;
};

/** Data for updating the design based on a user prompt */
export interface UpdateDesignPromptData {
	prompt: string;
}

/** Data for updating the design based on detailed instructions */
export interface UpdateDesignInstructionsData {
	instructions: string;
}

/** Configuration part of a CodeTask Preset (derived from CreateCodeTaskData) */
export type CodeTaskPresetConfig = Omit<CreateCodeTaskData, 'title' | 'instructions'>;
